package com.ruoyi.erecord.employee.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erecord.employee.domain.bo.EmployeeBo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeVo;
import com.ruoyi.erecord.employee.service.IEmployeeService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;

/**
 * 人员
 *
 * @author cc
 * @date 2023-04-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/employee")
public class EmployeeController extends BaseController {

    private final IEmployeeService iEmployeeService;
    /**
     * 查询人员列表
     */
    @SaCheckPermission("erecord:employee:list")
    @GetMapping("/list")
    public TableDataInfo<EmployeeVo> list(EmployeeBo bo, PageQuery pageQuery) {
        return iEmployeeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人员列表
     */
    @SaCheckPermission("erecord:employee:export")
    @Log(title = "人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeeBo bo, PageQuery pageQuery, HttpServletResponse response) {
    	pageQuery.setPageSize(10000);
    	TableDataInfo<EmployeeVo> list = iEmployeeService.queryPageList(bo, pageQuery);
        ExcelUtil.exportExcel(list.getRows(), "人员", EmployeeVo.class, response);
    }

    /**
     * 获取人员详细信息
     *
     * @param employeeCode 主键
     */
    @SaCheckPermission("erecord:employee:query")
    @GetMapping("/{employeeCode}")
    public R<EmployeeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long employeeCode) {
        return R.ok(iEmployeeService.queryById(employeeCode));
    }

    /**
     * 新增人员
     */
    @SaCheckPermission("erecord:employee:add")
    @Log(title = "人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmployeeBo bo) {
        return toAjax(iEmployeeService.insertByBo(bo));
    }

    /**
     * 修改人员
     */
    @SaCheckPermission("erecord:employee:edit")
    @Log(title = "人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeeBo bo) {
        return toAjax(iEmployeeService.updateByBo(bo));
    }

    /**
     * 删除人员
     *
     * @param employeeCodes 主键串
     */
    @SaCheckPermission("erecord:employee:remove")
    @Log(title = "人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employeeCodes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] employeeCodes) {
        return toAjax(iEmployeeService.deleteWithValidByIds(Arrays.asList(employeeCodes), true));
    }
    
}
